package com.boot.json.deserializer;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.deser.std.StringDeserializer;

import java.io.IOException;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @description 反序列化 将空字符串转化为null (需要手动加到属性上)
 * @date 2023/4/25 17:01
 **/

public class StringToNullDeserializer extends StdDeserializer<String> {

    public static final StringToNullDeserializer INSTANCE = new StringToNullDeserializer();

    protected StringToNullDeserializer() {
        super(String.class);
    }

    @Override
    public String deserialize(JsonParser parser, DeserializationContext context) throws IOException, JacksonException {
        String v = StringDeserializer.instance.deserialize(parser, context);
        if (v == null || v.isEmpty()) {
            return null;
        }
        return v;
    }
}
